package hlhj.fhp.supreme.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

import fhp.hlhj.giantfold.utils.LogUtil;
import hlhj.fhp.supreme.ClassPayEvent;
import hlhj.fhp.supreme.Contents;
import hlhj.fhp.supreme.R;
import hlhj.fhp.supreme.javabean.CartPayBean;
import hlhj.fhp.supreme.javabean.ChagePayEvent;
import hlhj.fhp.supreme.javabean.DaShangPayEvent;
import hlhj.fhp.supreme.javabean.JiePanDanRenEvent;
import hlhj.fhp.supreme.javabean.JiePanPayEvent;
import hlhj.fhp.supreme.javabean.JiePanTuanGouEvent;
import hlhj.fhp.supreme.javabean.PayfailEvent;
import hlhj.fhp.supreme.javabean.ReadPayEvent;
import hlhj.fhp.supreme.utils.MyUtils;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	

    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Contents.INSTANCE.getWXAPPKEY(),true);
    	api.registerApp(Contents.INSTANCE.getWXAPPKEY());
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
        LogUtil.INSTANCE.log("进入回调");
        PayReq payReq = (PayReq) req;
        LogUtil.INSTANCE.log("tag"+payReq.extData);
	}

	@Override
	public void onResp(BaseResp resp) {
        PayResp payResp = (PayResp) resp;
        LogUtil.INSTANCE.log("进入回调"+payResp.extData);
		Log.e("info", "onPayFinish, errCode = " + resp.errStr);
		Log.e("info", "微信支付 errCode = " + resp.errCode);
/*		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(R.string.app_tip);
			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
			builder.show();
		}*/

		if (resp.errCode==0){
            MyUtils.toast("支付成功");
			if (payResp.extData.equals("课堂支付")){
				EventBus.getDefault().postSticky(new ClassPayEvent());
			}else if (payResp.extData.equals("书籍支付")){
                EventBus.getDefault().postSticky(new ReadPayEvent());
            }else if (payResp.extData.equals("打赏")){
				EventBus.getDefault().postSticky(new DaShangPayEvent());
			}else if (payResp.extData.equals("充值")){
				EventBus.getDefault().postSticky(new ChagePayEvent());
			}else if (payResp.extData.equals("解盘支付")){
				EventBus.getDefault().postSticky(new JiePanPayEvent());
			}else if (payResp.extData.equals("购物车支付")){
				EventBus.getDefault().postSticky(new CartPayBean());
			} else if (payResp.extData.equals("解盘单人")){
				EventBus.getDefault().postSticky(new JiePanDanRenEvent());
			}else if (payResp.extData.equals("解盘团购")){
				LogUtil.INSTANCE.log("进入回调");
				EventBus.getDefault().postSticky(new JiePanTuanGouEvent());
			}
		}else {
			MyUtils.toast("支付失败");
			EventBus.getDefault().postSticky(new PayfailEvent());
		}

		finish();
	}
}